define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Lease, GoAddress,GoDialog, $stateParams, $window, $timeout, $ionicSlideBoxDelegate, $ionicSideMenuDelegate, GoPage, GoCatTypeAuth, Auth) {
		//商品分类:二手产品为9，租赁产品为10
		$scope.cat_type = $stateParams.cat_type;
		//初始值设置
		//默认排序为价格从高到低:4。价格从低到高:5
		$scope.input = new Object();
		$scope.input.key = '';
		$scope.input.nPage = 1;
		$scope.orderby = 1;
		//地区格式为字符串，形如：region_A_01,region_B_01,region_C_01
		$scope.region = '';
		//品牌格式为你字符串，形如：store_cat_A_01,store_cat_B_01,store_cat_C_01
		$scope.store_cat = '';
		$scope.act_id = '';
		$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
		$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
		$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.datas = new Object();
		$scope.count = null;
		//动态设置高度
		$scope.$on('$ionicView.afterEnter', function(){
				var lan = 0; //这是状态栏，只有ios的APP才有
		var tab = 0;
		var bars = document.getElementById("nav-bar-title");
		var bar = ((bars != undefined) ? 44 : 0);
		if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
		//整体高度
		var all = $window.innerHeight;
		var lease_top1 = document.getElementById("lease-top1").clientHeight;
		$scope.lease_myObj = { //滚动条可视范围
			"height": all - lan - bar - tab - lease_top1 + "px"
		};
			},false)
		$scope.isLoad = false;
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function(data, obj) {
			$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
			$scope.input.district_items = null;
		};
		$scope.listen_city = function(data, obj) {
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		};

		//定义绑定
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);
		/***分类三级联动***/
		$scope.input.brand_id = "0"; //这是品牌下拉框的绑定数据
		$scope.input.store_id = Auth.get_user_id(); //隐藏的企业id的绑定数据

		$scope.input.cat1 = { id: 0, name: "请选择大分类" };
		$scope.input.cat2 = { id: 0, name: "请选择中分类" };
		$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_cat1 = function(data) {
			$scope.input.cat2 = { id: 0, name: "请选择大分类" };
			$scope.input.cat3 = { id: 0, name: "请选择中分类" };
			$scope.cat3_items = null;
		};
		$scope.listen_cat2 = function(data) {
			$scope.input.cat3 = { id: 0, name: "请选择中分类" };
		};
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat, "0");

		$scope.isHide = true; //默认隐藏
		//隐藏弹窗
		$scope.hide_apply = function() {
			$scope.isHide = true;
		}
		//显示弹窗
		$scope.show_apply = function() {
			$scope.isHide = false;
		}

		//		$scope.isLoad = false; //可以加载
		//画面加载
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
			//判断关闭
			if($ionicSideMenuDelegate.isOpenRight()) {
				$ionicSideMenuDelegate.toggleRight();
			}
		};
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Lease.getlist(1, 10, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function(data) {
				if(data.data.length > 0)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			if($scope.datas.data == undefined)
				$scope.input.nPage = 0;
			else
			if($scope.datas.page.count <= $scope.datas.data.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				if($scope.datas.page.count<$scope.input.nPage*20){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *20;
				}else{
					$scope.count = null;
				}
			}
			Lease.getlist($scope.input.nPage + 1, 10, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function(data) {
				if(data.data.length > 0) {
					if($scope.datas.data != undefined) {
						if($scope.count!=null)
						$scope.datas.data.splice($scope.count,$scope.datas.page.count-$scope.count);
						$scope.datas.data = $scope.datas.data.concat(data.data);
						$scope.input.nPage += 1;
					} else {
						$scope.datas = data;
						$scope.input.nPage = 1;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		//查询
		$scope.get_brand = function() {
			$scope.store_cat = '';
			if($scope.input.cat1&&$scope.input.cat1.id!=0) {
				$scope.store_cat = $scope.input.cat1.id;
			}
			if($scope.input.cat2&&$scope.input.cat2.id!=0) {
				$scope.store_cat += ',' + $scope.input.cat2.id;
			}
			if($scope.input.cat3&&$scope.input.cat3.id!=0) {
				$scope.store_cat += ',' + $scope.input.cat3.id;
			}
		}
		$scope.get_region = function() {
			$scope.region = '';
			if($scope.input.province&&$scope.input.province.id!=0) {
				$scope.region = $scope.input.province.id;
			}
			if($scope.input.city&&$scope.input.city.id!=0) {
				$scope.region = $scope.input.province.id + ',' + $scope.input.city.id;
			}
			if($scope.input.district&&$scope.input.district.id!=0) {
				$scope.region = $scope.input.province.id + ',' + $scope.input.city.id + ',' + $scope.input.district.id;
			}
		}
		//查询
		$scope.doSearch = function(orderby) {
			if(orderby != undefined)
				$scope.orderby = orderby;
			$scope.get_brand();
			$scope.get_region();
			Lease.getlist(1, 10, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function(data) {
				if(data.data.length > 0)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			});
			$ionicSideMenuDelegate.toggleRight();

		};
		//取消
		$scope.doCancel = function() {
			$ionicSideMenuDelegate.toggleRight();
		};

		$scope.onshow_post = function() {
			GoPage.go_states("newproductpost");
		}

		$scope.onshow_statistics = function() {
			GoPage.go_states("statistics");
		}
		$scope.onshow_start = function() {
			GoPage.go_states("statistics");
		}
		$scope.init_data = function() {
			Lease.getlist(1, 10, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function(data) {
				if(data.data.length > 0)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
//				console.log(data);
			})
		}
		$scope.onShow_detail = function(goods_id) {
			GoPage.go_states("productdetail", { "goods_id": goods_id, "type": "goods" })
		}
		$scope.onshow_first = function(store_id) {
			GoPage.go_states("first", { store_id: store_id });
		}
		$scope.clear_key = function(){
			$scope.input.key = '';
		}
		$scope.init_data();
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
	}

	controller.$inject = ['$scope', 'Lease', 'GoAddress','GoDialog', '$stateParams', '$window', '$timeout', '$ionicSlideBoxDelegate', '$ionicSideMenuDelegate', 'GoPage', 'GoCatTypeAuth', 'Auth'];

	return app.registerController("leasectrl",controller);
});